//
//  SDPhotoGroup.m
//  二级评论
//
//  Created by Andrew on 2018/1/22.
//  Copyright © 2018年 Andrew. All rights reserved.
//

#import "SDPhotoGroup.h"

@implementation SDPhotoGroup

#define SDPhotoGroupImageMargin 15
#define SDstatusPhotoMargin 10
#define SDstatusPhotoMaxCol 4
#define SDstatusPhotoWH (SCREENWIDTH - SDstatusPhotoMargin * (SDstatusPhotoMaxCol + 1)) / SDstatusPhotoMaxCol

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (void)setPhotoItemArray:(NSArray *)photoItemArray
{
    _photoItemArray = photoItemArray;
    
     [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; //清除子控件
    for (NSInteger i = 0; i < photoItemArray.count; i++) {
        
        UIImageView *image = [[UIImageView alloc] init];
        
        NSInteger imageCount = self.photoItemArray.count;
        NSInteger rowIndex = i / imageCount;
        NSInteger columnIndex = i % imageCount;
        CGFloat w = SDstatusPhotoWH;
        CGFloat h = SDstatusPhotoWH;
        CGFloat x = columnIndex * (w + SDstatusPhotoMargin) + SDstatusPhotoMargin;
        CGFloat y = rowIndex * (h + SDPhotoGroupImageMargin);
        image.frame = CGRectMake(x, y, w, h);
        
        image.clipsToBounds = YES;
        image.layer.cornerRadius = 5.0f;
        image.backgroundColor = YMColor(236, 236, 236);
        NSString *objStr = [NSString stringWithFormat:@"%@",photoItemArray[i]];
        image.image = [UIImage imageNamed:objStr];
        image.clipsToBounds = YES;
        image.contentMode = UIViewContentModeScaleAspectFill;
        image.userInteractionEnabled = YES;
        image.tag = i;
        [self addSubview:image];
    }
}

+ (CGSize)sizeWithCount:(NSUInteger)count
{
    // 最大列数（一行最多有多少列）
    int maxCols = SDstatusPhotoMaxCol;
    
    //列数
    NSUInteger cols = maxCols;
    CGFloat photosW = cols * SDstatusPhotoWH + (cols - 1) * SDstatusPhotoMargin;
    
    // 行数
    NSUInteger rows = (count + maxCols - 1) / maxCols;
    CGFloat photosH = rows * SDstatusPhotoWH + (rows - 1) * SDstatusPhotoMargin;
    
    return CGSizeMake(photosW, photosH);
}

@end
